import Mock from 'mockjs'
import { toAnyString } from '@/utils'
import treedata from "../data/treedata.json"
import treeSelectdata from "../data/treeselectdata.json"

let List: any[] = []
const count = 100

const baseContent = '<p>I am testing data, I am testing data.</p><p><img src="https://wpimg.wallstcn.com/4c69009c-0fd4-4153-b112-6cb53d1cf943"></p>'
// const image_uri = 'https://wpimg.wallstcn.com/e4558086-631c-425c-9430-56ffb46e70b3'

for (let i = 0; i < count; i++) {
  List.push(Mock.mock({
    id: toAnyString(),
    // timestamp: +Mock.Random.date('T'),
    author: '@first',
    title: '@title(5, 10)',
    content: baseContent,
    importance: '@integer(1, 3)',
    display_time: '@datetime',
    pageviews: '@integer(300, 5000)'
    // image_uri
  }))
}

export default [
  // 列表接口
  {
    url: 'http://mockjs.test.cn/example/list',
    type: 'get',
    response: (config: any) => {
      const { title, pageIndex, pageSize } = config.query

      const mockList = List.filter(item => {
        if (title && item.title.indexOf(title) < 0) return false
        return true
      })
      const pageList = mockList.filter((item, index) => index < pageSize * pageIndex && index >= pageSize * (pageIndex - 1))

      return {
        code: '0000',
        data: {
          total: mockList.length,
          list: pageList
        }
      }
    }
  },

     // 树形搜索
     {
      url: 'http://mockjs.test.cn/example/treeSelectData',
      type: 'get',
      response: (config: any) => {
        const { title, pageIndex, pageSize } = config.query
        const pageList = treeSelectdata
  
        return {
          code: '0000',
          data: {
            list: pageList
          }
        }
      }
    },

   // 查询树表格
   {
    url: 'http://mockjs.test.cn/example/treeData',
    type: 'get',
    response: (config: any) => {
      const { title, pageIndex, pageSize } = config.query
      const pageList = treedata

      return {
        code: '0000',
        data: {
          list: pageList
        }
      }
    }
  },

  // 嵌套表格数据
  {
    url: 'http://mockjs.test.cn/example/expendData',
    type: 'get',
    response: (config: any) => {
      const { id } = config.query

     const expendMap={
       "1":[
        { name: '丘处机', age: 56, home: '武当山' },
        { name: '刘处玄', age: 47, home: '武当山' },
        { name: '王处一', age: 23, home: '武当山' }
      ],
      "2": [
        { name: '梅超风', age: 44, home: '桃花岛' },
        { name: '黄蓉', age: 27, home: '桃花岛' },
        { name: '傻姑', age: 21, home: '桃花岛' }
      ],
      "3":[
        { name: '欧阳克', age: 29, home: '西域' },
        { name: '杨过', age: 21, home: '桃花岛' }
      ],
      "4": [
        { name: '郭靖', age: 28, home: '牛家村' },
        { name: '黄蓉', age: 27, home: '桃花岛' }
      ]
     }


      return {
        code: '0000',
        data: {
          list: expendMap[id]
        }
      }
    }
  },


]
